package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.entity.SysAppPermission;
import com.lu.statistics.entity.front.response.UserPermissionResponse;
import com.lu.statistics.entity.vo.UserPermissions;
import com.lu.statistics.mapper.SysAppPermissionMapper;
import com.lu.statistics.mapper.SysAppRolePermissionMapper;
import com.lu.statistics.service.PermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl extends ServiceImpl<SysAppPermissionMapper, SysAppPermission> implements PermissionService {
    @Autowired
    private SysAppRolePermissionMapper rolePermissionMapper;

    @Override
    public List<UserPermissions> queryUserPermMenuList(Integer userId, String type) {
        List<UserPermissions> permissionsList = rolePermissionMapper.queryUserPermissionListByUserId(userId, 1, type, 2, "SELECT");
        return permissionsList;
    }

    @Override
    public List<UserPermissions> queryRolePermMenuList(String roleCode, String type) {
        List<UserPermissions> permissionsList = rolePermissionMapper.queryRolePermissionListByCode(roleCode, 1, type, 2, "SELECT");
        return permissionsList;
    }

    @Override
    public List<UserPermissionResponse> getUserPermMenus(String roleCode, String type) {
        List<UserPermissionResponse> newList = new ArrayList<>();
        List<UserPermissions> permissionsList = rolePermissionMapper.queryRolePermissionListByCode(roleCode, 1, type, 2, "SELECT");
        if (permissionsList != null && permissionsList.size() > 0) {
            for (int i = 0; i < permissionsList.size(); i++) {
                UserPermissions permissions = permissionsList.get(i);
                UserPermissionResponse permissionResponse = new UserPermissionResponse();
                String url = "/api/file/previewPhoto?fileFullPath=" + permissions.getIcon();
                BeanUtils.copyProperties(permissions, permissionResponse);
                permissionResponse.setId(i + 1);
                permissionResponse.setMenuId(permissions.getId());
                permissionResponse.setIcon(url);
                newList.add(permissionResponse);
            }
        }
        return newList;
    }
}
